package controllers

import (
	"encoding/json"
	_ "gitchina/kly_service/errors"
	"gitchina/kly_service/models"

	"github.com/astaxie/beego"
)

type SocialController struct {
	beego.Controller
}

// @router /108001 [post]
func (o *SocialController) CreateChatSession() {
	var rq models.ChatRQ
	var social models.Social
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if key, err := social.CreateChatSession(string(mbrId), rq.TargetId); err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(key)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /108002 [post]
func (o *SocialController) AddChatLog() {
	var rq models.ChatRQ
	var social models.Social
	mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		if id, err := social.AddChatLog(string(mbrId), rq.SessionKey, rq.Text, rq.MessageId, 1); err != nil {
			o.Data["json"] = models.NewFailResponse(err)
		} else {
			o.Data["json"] = models.NewOkResponse(id)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /108003 [post]
func (o *SocialController) GetChatLogs() {
	var rq models.ChatRQ
	var social models.Social
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		rs := social.GetChatLogs(rq.SessionKey, rq.Offset)
		o.Data["json"] = models.NewOkResponse(rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /108004 [post]
func (o *SocialController) GetChatContacts() {
	var rq models.GetChatContactsRQ
	var social models.Social
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		mbrId := models.ID(o.Ctx.Input.GetData("mbrId").(string))
		rs := social.GetChatContacts(string(mbrId), rq.PageNo, rq.PageSize)
		o.Data["json"] = models.NewOkResponse(rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /108005 [post]
func (o *SocialController) SendMessage() {
	var rq models.SendMessageRQ
	var social models.Social
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		from := o.Ctx.Input.GetData("mbrId").(string)
		nickName := o.Ctx.Input.GetData("nickName").(string)
		if err := social.SendMessage(from, nickName, rq.SessionKey, rq.Text, rq.MediaType); err == nil {
			o.Data["json"] = models.NewOkResponse(nil)
		} else {
			o.Data["json"] = models.NewFailResponse(err)
		}
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /108006 [post]
func (o *SocialController) GetLatestChatLogs() {
	var rq models.GetChatLogListRQ
	var social models.Social
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &rq); err == nil {
		rs := social.GetLatestChatLogs(rq.SessionKey, rq.PageNo, rq.PageSize)
		o.Data["json"] = models.NewOkResponse(rs)
		o.ServeJSON()
	} else {
		o.Ctx.ResponseWriter.WriteHeader(400)
	}
}
